<template>
	<view class="box">
		<view class="title">
			点击修改头像和昵称
		</view>

		<button class="avater-wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
			<image :src="avatarUrl" mode=""></image>
		</button>
		<input @blur="savenickname" type="nickname" class="weui-input" placeholder="请输入昵称" />
		<button class="save" @click="saveDate">保存</button>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				avatarUrl: '/static/moren.png',
				nickName: ''
			};
		},
		computed: {
			...mapState('m_user', ['userinfo'])
		},
		methods: {
			...mapMutations('m_user', ['saveUserInfo']),
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail
				this.avatarUrl = avatarUrl
			},
			savenickname(e) {
				// console.log(e);
				this.nickName = e.detail.value
			},
			saveDate() {
				// console.log(this.avatarUrl)
				// console.log(this.nickName)
				if (this.nickName === '') return uni.$showMsg('昵称不能为空！')
				this.userinfo.avatarUrl = this.avatarUrl
				this.userinfo.nickName = this.nickName
				this.saveUserInfo(this.userinfo)
				// console.log(this.userinfo);

				uni.navigateBack({
					url: '/pages/my/my'
				})
				uni.$showMsg('修改成功！')

			}
		}
	}
</script>

<style lang="scss">
	.box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		.title {
			margin: 20px 0;
			font-size: 18px;
			font-weight: bold;
		}

		.avater-wrapper {
			width: 100px;
			height: 100px;
			border-radius: 50%;
			border: 2px #fff solid;
			box-shadow: 1px 2px 5px #f4f4f6;
			padding: 0;

			image {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		}

		.weui-input {
			margin: 20px 0;
			border-top: 1px #f4f4f6 solid;
			border-bottom: 1px #f4f4f6 solid;
			height: 50px;
			width: 600rpx;
		}

		.save {
			background-color: #c00000;
			color: #fff;
			font-size: 18px;
			font-weight: bold;
			width: 500rpx;
			height: 50px;
			margin-top: 30px;
			border-radius: 25px;
		}
	}
</style>
